package com.yn.admin.zy;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yn.common.enums.AuditStatusEnum;
import com.yn.common.enums.CarStatusEnum;
import com.yn.common.model.zy.TCarManageDO;
import com.yn.common.page.BasePage;
import com.yn.common.request.sc.CarFleetManageRequest;
import com.yn.common.request.sc.CarManageRequest;
import com.yn.common.response.sc.BaseResult;
import com.yn.service.zy.TCarManageService;
import com.yn.service.zy.model.dto.TCarManageDTO;
import com.yn.service.zy.model.query.TCarManageQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/tcarmanage")
@Api(tags = "车辆-司机 相关接口")
public class TCarManageController {
    @Autowired
    private TCarManageService tCarManageService;

    /**
     * 分页获取车辆-司机信息
     * @param query
     * @param basePage
     * @return
     */
    @GetMapping("/list-page")
    @ApiOperation("分页获取车辆-司机信息")
    public BaseResult listPage(TCarManageQuery query, BasePage basePage){
        IPage<TCarManageDTO> tCarManageDTOIPage = tCarManageService.listPage(query,basePage);

        return BaseResult.success(tCarManageDTOIPage);
    }

    /**
     * 展示所有车队名称——用于添加司机选择车队
     * key——id
     * value——车队名称
     * */

    @ApiOperation("展示所有车队名称")
    @GetMapping("/fleet/info")
    public BaseResult<Map<Integer, String>> getFleetName() {
        return BaseResult.success(tCarManageService.getFleetName());
    }

    /**
     * 获取车辆-司机详情
     * @param id
     * @return
     */
    @GetMapping("/info")
    @ApiOperation("获取车辆-司机详情")
    public BaseResult<TCarManageDTO> info(@RequestParam(value = "id",required = true)
                           @NotNull(message = "请选择")
                           @ApiParam(value = "主键id",required = true) Integer id){
		TCarManageDTO tCarManageDTO = tCarManageService.getInfo(id);

        this.transformParams(tCarManageDTO);

        return BaseResult.success(tCarManageDTO);
    }

    /**
     * 批量审核
     * @param carIds
     * @param auditStatus
     * @return
     */
    @PostMapping("/batch-audit")
    @ApiOperation("批量审核")
    public BaseResult batchAudit(@RequestBody List<Integer> carIds,
                                @ApiParam(value = "审核状态",required = true)
                                 @RequestParam(value = "auditStatus",required = true)
                                 @NotNull(message = "请选择审核装填") Integer auditStatus){
        tCarManageService.batchAudit(carIds,auditStatus);
        return BaseResult.success();
    }

    /**
     * 添加司机
     * @param request
     * @param fleetManageId
     */
    @PostMapping("/save")
    @ApiOperation(value = "添加司机")
    public BaseResult<String> save(@RequestParam(value = "fleetManageId",required = true) Integer fleetManageId,
            @RequestBody @Validated CarManageRequest request){
        tCarManageService.saveDriver(request,fleetManageId);
        return BaseResult.success();
    }



    /**
     * 翻译字段
     * @param tCarManageDTO
     */
    private void transformParams(TCarManageDTO tCarManageDTO){
        if (ObjectUtils.isEmpty(tCarManageDTO)) {
            return;
        }
        if (!ObjectUtils.isEmpty(tCarManageDTO.getCarStatus())) {
            CarStatusEnum carStatusEnum = CarStatusEnum.getByCode(tCarManageDTO.getCarStatus());
            if (!ObjectUtils.isEmpty(carStatusEnum)) {
                tCarManageDTO.setCarStatusStr(carStatusEnum.getName());
            }
        }

        if (!ObjectUtils.isEmpty(tCarManageDTO.getAuditStatus())) {
            AuditStatusEnum auditStatusEnum = AuditStatusEnum.getByCode(tCarManageDTO.getAuditStatus());
            if (!ObjectUtils.isEmpty(auditStatusEnum)) {
                tCarManageDTO.setAuditStatusStr(auditStatusEnum.getName());
            }
        }
    }
}
